#define _CRT_SECURE_NO_WARNINGS  1
#include"Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);

	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, datatype x)
{
	assert(pq);
	QNode* new = (QNode*)malloc(sizeof(QNode));
	if (new==NULL)
	{
	  perror("malloc fail\n");
	  return;
	}
	  new->data = x;
      new->next = NULL;

	if (pq->head == NULL)
	{
		assert(pq->tail == NULL);

		pq->head = new;
		pq->tail = new;
	}
	else
	{
		pq->tail->next = new;
		pq->tail = new;
	}

	pq->size++;


}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);
	
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;

	}

	pq->size--;

}
datatype QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->data;
}
datatype QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->data;
}
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	if (pq->size == 0)
	{
		return true;
	}
	return false;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);

	QNode* cur = pq->head;
	while (cur)
	{
		QNode*next=cur->next;
		free(cur);
		cur = next;
	}

	pq->head = pq->tail = NULL;
	pq->size = 0;
}